package cn.caplike.demo.spring.bean.lifecycle.aware.interfaces;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * A Demo of Aware Interfaces
 *
 * @author LiKe
 */
@Component("awareBean")
public class AwareBeanImpl implements
        /*
         * The setApplicationContext() method of this interface provides the ApplicationContext object of this bean.
         */
        ApplicationContextAware,
        /*
         * The setBeanName() callback of this interface supplies the name of the bean.
         */
        BeanNameAware,
        /*
         * Provides setBeanFactory(), a callback that supplies the owning factory to the bean instance.
         */
        BeanFactoryAware {
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("setBeanFactory method of AwareBeanImpl is called");
        System.out.println("setBeanFactory:: AwareBeanImpl singleton= " + beanFactory.isSingleton("awareBean"));
    }

    @Override
    public void setBeanName(String beanName) {
        System.out.println("setBeanName method of AwareBeanImpl is called");
        System.out.println("setBeanName:: Bean Name defined in context= " + beanName);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("setApplicationContext method of AwareBeanImpl is called");
        System.out.println("setApplicationContext:: Bean Definition Names= " + Arrays.toString(applicationContext.getBeanDefinitionNames()));
    }
}
